package com.nowcoder.community.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;

import java.util.Map;
import java.util.UUID;

public class CommunityUtils {

    /*
    * 生成随机字符串
    * */
    public static String generateUUID(){
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /*
    * md5 加密
    * */
    public static String md5(String password, String salt){
        if (StringUtils.isBlank(password)){
            return null;
        }
        return DigestUtils.md5DigestAsHex((password + salt).getBytes());
    }

    // 生成 JSON 字符串
    public static String getJsonString(int code, String msg, Map<String, Object> map){
        JSONObject json = new JSONObject();
        json.put("code", code);
        json.put("msg", msg);
        if (map != null){
            map.forEach((k, v) -> json.put(k, v));
        }
        return json.toJSONString();
    }

    public static String getJsonString(int code, String msg){
        return getJsonString(code, msg, null);
    }

    public static String getJsonString(int code){
        return getJsonString(code, null, null);
    }

}
